package com.tjbank.cssys.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tjbank.cssys.api.CkOutsourcingCompanyService;
import com.tjbank.cssys.dto.CkOutsourcingCompanyDTO;

import com.tjbank.cssys.web.config.GetUserCodeFromToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.tjbank.cssys.framework.base.api.BaseController;
import com.tjbank.cssys.framework.base.api.Response;
import com.tjbank.cssys.api.OrganService;
import com.tjbank.cssys.dto.OrganDTO;

import org.springframework.web.bind.annotation.*;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 机构管理
 * <p>
 * Package : com.tansun.magicube.iam.web.controller
 *
 * @author -- lijiangtao 2020年6月28日 下午4:53:05
 */
@RestController
public class OrganController extends BaseController {

    @Autowired
    private OrganService organService;
    @Autowired
    private CkOutsourcingCompanyService ckOutsourcingCompanyService;
    private static final Logger logger = LoggerFactory.getLogger(OrganController.class);

    /**
     * 新增机构
     *
     * @param organizationDTO
     * @return
     * @author -- lijiangtao 2020年5月21日 下午8:32:54
     */
    @PostMapping("/organ")
    public Response<?> addOrgan(@RequestBody @Valid OrganDTO organDTO, HttpServletRequest request) {

        return this.success(organService.addOrgan(organDTO));
    }

    /**
     * 新增机构和委外公司
     *
     * @param
     * @return
     * @author -- lijiangtao 2020年5月21日 下午8:32:54
     */
    @PostMapping("/organAndOutsourcingCompany")
    public Response<?> addOrganAndOutsourcingCompany(@RequestBody Map<String, Object> params, HttpServletRequest request) {


        CkOutsourcingCompanyDTO ckOutsourcingCompanyDTO = new CkOutsourcingCompanyDTO();


        ckOutsourcingCompanyDTO = JSONObject.parseObject(JSONObject.toJSONString(params), CkOutsourcingCompanyDTO.class);
        String acceptLevel = String.valueOf(params.get("acceptLevel"));
        Object backRateList = params.get("backRateList");
        JSONArray rateList = JSONObject.parseArray(backRateList.toString());
        ckOutsourcingCompanyService.addOutsourcingCompany(ckOutsourcingCompanyDTO, acceptLevel, rateList);

        return success();
    }

    /**
     * 删除机构
     *
     * @param nodeId
     * @return
     * @author -- lijiangtao 2020年7月14日 下午5:21:17
     */
    @DeleteMapping("/organ/{nodeId}")
    public Response<?> deleteOrgan(@PathVariable String nodeId, HttpServletRequest request) {


        return this.success(organService.deleteOrgan(nodeId));
    }

    /**
     * 查询机构信息list-->tree
     *
     * @return
     * @author -- lijiangtao 2020年7月14日 下午5:31:32
     */
    @GetMapping("/organ/tree")
    public Response<?> organTreeList(OrganDTO organDTO, HttpServletRequest request) {


        return this.success(organService.organTreeList(organDTO));
    }

    /**
     * 分页查询用户信息
     *
     * @param organId
     * @param pageable
     * @return
     * @author -- lijiangtao 2020年7月27日 下午5:22:49
     */
    @GetMapping("/organ/user")
    public Response<?> queryUserByOrgId(String organId, Pageable pageable, HttpServletRequest request) {

        return this.success(organService.queryUserByNodeId(organId, pageable));
    }

    /**
     * 角色授权 ----> 数据权限
     *
     * @param organDTO
     * @return
     * @author -- lijiangtao 2020年7月31日 下午12:53:03
     */
    @GetMapping("/organ/dataPrem")
    public Response<?> organOrgTreeByTenantCode(OrganDTO organDTO, HttpServletRequest request) {

        return this.success(organService.organOrgTreeByTenantCode(organDTO));
    }

    /**
     * 更新机构
     *
     * @param userGroupDTO
     * @return
     * @author -- lijiangtao 2020年5月31日 下午11:02:51
     */
    @PutMapping("/organ")
    public Response<?> updateOrgan(@RequestBody @Valid OrganDTO organDTO, HttpServletRequest request) {

        return this.success(organService.updateOrgan(organDTO));
    }


    @GetMapping("/organ/getCodeOrName")
    public Response<Map<String, String>> getCodeOrName(@RequestParam Map<String, String> map, HttpServletRequest request) {

        return this.success(organService.getCountByCodeOrName(map));
    }


}
